﻿using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.ReSharper.Psi.CSharp.Tree;
using Twins.Classes.Hanlder.HanlderExpression;
using Twins.Interface.IHandler;

namespace Twins.Classes.HanlderRegistry
{
    public class CSharpExpressionHanlderRegistry
    {
        private static readonly Dictionary<Type, Type> Handlers = InitHanlders();

        private static Dictionary<Type, Type> InitHanlders()
        {
            return new Dictionary<Type, Type>
            {
               {typeof (IInvocationExpression), typeof(HanlderForIInvocationExpression)},
               {typeof (IAssignmentExpression), typeof(HanlderForIAssignmentExpression)},
               {typeof (IAdditiveExpression), typeof(HanlderForIAdditiveExpression)},
               {typeof (IConditionalAndExpression), typeof(HanlderForIConditionalAndExpression)},
               {typeof (IEqualityExpression), typeof(HanlderForIEqualityExpression)},
               {typeof (IPostfixOperatorExpression), typeof(HanlderForIPostfixOperatorExpression)},
               {typeof (IRelationalExpression), typeof(HanlderForIRelationalExpression)}
            };
        }

        public static IHandler Get(object cSharpExpression)
        {
            IEnumerable<IHandler> handlers = from type in Handlers.Keys
                                             where type.IsAssignableFrom(cSharpExpression.GetType())
                                             select Activator.CreateInstance(Handlers[type]) as IHandler;
            return handlers.FirstOrDefault();
        }
         
    }
}